package components
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;

	public class Sonido
	{
		private var sound:Sound;
		private var soundChannel:SoundChannel;
		private var playing:Boolean;
		private var replay:Boolean;

		private function _completeHandler() : void
		{
			playing = false;
			if (replay) {
				play(true);
			}
		}

		public function Sonido(url: String)
		{
			playing = false;
			sound = new Sound(new URLRequest(url));
		}

		public function play(r : Boolean = false) : void
		{
			if (playing) {
				stop();
			}
			soundChannel = sound.play();
			soundChannel.addEventListener(Event.SOUND_COMPLETE, _completeHandler);
			playing = true;
			replay = r;
		}

		public function isPlaying() : Boolean
		{
			return playing;
		}

		public function stop() : void
		{
			if (!playing) {
				return;
			}
			soundChannel.stop();
			playing = false;
		}
	}
}